using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace Buccaneer
{
	public class BitmapButton : Button
	{
		enum btnState
		{
			BUTTON_UP=0,
			BUTTON_DOWN=1,
			BUTTON_FOCUSED=2,
			BUTTON_MOUSE_ENTER=3,
			BUTTON_DISABLED=4,
		}

		btnState imgState=btnState.BUTTON_UP;
		bool mouseEnter=false;

        public BitmapButton()
        {
            // enable double buffering.  Must be done by a derived class
            SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);

            // initialize event handlers
            Paint += new PaintEventHandler(BitmapButton_Paint);
            MouseDown += new MouseEventHandler(BitmapButton_MouseDown);
            MouseUp += new MouseEventHandler(BitmapButton_MouseUp);
            GotFocus += new EventHandler(BitmapButton_GotFocus);
            LostFocus += new EventHandler(BitmapButton_LostFocus);
            MouseEnter += new EventHandler(BitmapButton_MouseEnter);
            MouseLeave += new EventHandler(BitmapButton_MouseLeave);
            KeyDown += new KeyEventHandler(BitmapButton_KeyDown);
            KeyUp += new KeyEventHandler(BitmapButton_KeyUp);
            EnabledChanged += new EventHandler(BitmapButton_EnabledChanged);
        }

		private void BitmapButton_Paint(object sender, PaintEventArgs e)
		{
			Graphics gr=e.Graphics;
			int indexWidth=Size.Width*(int)imgState;

			if (Image.Width > indexWidth)
			{
				gr.DrawImage(Image, 0, 0, new Rectangle(new Point(indexWidth, 0), Size), GraphicsUnit.Pixel);
			}
			else
			{
				gr.DrawImage(Image, 0, 0, new Rectangle(new Point(0, 0), new Size(Size.Width, Size.Height)), GraphicsUnit.Pixel);
			}
		}

        private void BitmapButton_MouseDown(object sender, MouseEventArgs e)
		{
			imgState=btnState.BUTTON_DOWN;
			Invalidate();
		}

		private void BitmapButton_MouseUp(object sender, MouseEventArgs e)
		{
			imgState=btnState.BUTTON_FOCUSED;
			Invalidate();
		}

		private void BitmapButton_GotFocus(object sender, EventArgs e)
		{
            imgState=btnState.BUTTON_FOCUSED;
			Invalidate();

            Parent.Controls[0].Focus();
		}

		private void BitmapButton_LostFocus(object sender, EventArgs e)
		{
			if (mouseEnter)
			{
				imgState=btnState.BUTTON_MOUSE_ENTER;
			}
			else
			{
				imgState=btnState.BUTTON_UP;
			}
			Invalidate();
		}

		private void BitmapButton_MouseEnter(object sender, EventArgs e)
		{
			// only show mouse enter if doesn't have focus
			if (imgState==btnState.BUTTON_UP)
			{
				imgState=btnState.BUTTON_MOUSE_ENTER;
			}
			mouseEnter=true;
			Invalidate();
		}

		private void BitmapButton_MouseLeave(object sender, EventArgs e)
		{
			// only restore state if doesn't have focus
			if (imgState != btnState.BUTTON_FOCUSED)
			{
				imgState=btnState.BUTTON_UP;
			}
			mouseEnter=false;
			Invalidate();
		}

		private void BitmapButton_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyData==Keys.Space)
			{
				imgState=btnState.BUTTON_DOWN;
				Invalidate();
			}
		}

		private void BitmapButton_KeyUp(object sender, KeyEventArgs e)
		{
			if (e.KeyData==Keys.Space)
			{
				// still has focus
				imgState=btnState.BUTTON_FOCUSED;
				Invalidate();
			}
		}

		private void BitmapButton_EnabledChanged(object sender, EventArgs e)
		{
			if (Enabled)
			{
				imgState=btnState.BUTTON_UP;
			}
			else
			{
				imgState=btnState.BUTTON_DISABLED;
			}
			Invalidate();
		}

        private VisualStyleRenderer renderer;
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (this.Focused && Application.RenderWithVisualStyles && this.FlatStyle == FlatStyle.Standard)
            {
                if (renderer == null)
                {
                    VisualStyleElement elem = VisualStyleElement.Button.PushButton.Normal;
                    renderer = new VisualStyleRenderer(elem.ClassName, elem.Part, (int)PushButtonState.Normal);
                }
            }
        }
	}
}